package com.sgb.goods.view.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.goods.BR;
import com.sgb.goods.BuildConfig;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityStorageManageBinding;
import com.sgb.goods.entity.StorageManageCallBackEntity;
import com.sgb.goods.entity.StorageManageEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.utils.helper.SmartRefreshLayoutHelper;
import com.sgb.goods.utils.module.GoodsModule;
import com.sgb.goods.viewmodel.StorageManageViewModel;
import com.swgk.core.dialog.RoundCornerDialog;

/**
 * 描述：商品-仓储管理
 *
 * @author qiangHeNoo
 * <p>
 * 时间：2021.11.27
 */
@Route(path = Constants.ARouter.AR_STORAGE_MANAGER)
public class StorageManageActivity extends BaseActivity<GoodsActivityStorageManageBinding, StorageManageViewModel>
        implements SmartRefreshLayoutHelper {

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(StorageManageViewModel.class, BR.viewModel);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_storage_manage;
    }

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, StorageManageActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mViewModel.getStorageList(false);
    }

    @Override
    protected void init() {
        initView();
    }

    @Override
    protected void initListener() {
        mDataBinding.headContainer.ivBack.setOnClickListener(v -> finish());
        mDataBinding.rlvLayout.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mViewModel.getStorageList(false);
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mViewModel.getStorageList(true);
            }
        });
        mDataBinding.goodsTvAdd.setOnClickListener(v -> {
            if (isHavingPermission()) {
                GoodsAddStorageActivity.start(this, null);
            }
        });
        mViewModel.adapter.setOnItemChildClickListener((adapter, view, position) -> {
            StorageManageEntity entity = (StorageManageEntity) view.getTag();
            if(view.getId() == R.id.goods_tv_delete) {
                mViewModel.deleteCallBackEntity.postValue(new StorageManageCallBackEntity(entity, position));
            } else if(view.getId() == R.id.goods_tv_edit) {
                mViewModel.editCallBackEntity.postValue(new StorageManageCallBackEntity(entity, position));
            }
        });
    }

    @Override
    protected void initObserve() {
        mViewModel.listMutableLiveData.observe(this, storageManageEntities -> {
            refreshUi(mDataBinding.rlvLayout.refreshLayout, mViewModel.adapter, storageManageEntities);
        });
        mViewModel.deleteCallBackEntity.observe(this, storageManageCallBackEntity -> {
            if (isHavingPermission()) {
                showDialog(storageManageCallBackEntity.getStorageManageEntity().getId(), storageManageCallBackEntity.getPosition());
            }
        });
        mViewModel.editCallBackEntity.observe(this, storageManageCallBackEntity -> {
            if (isHavingPermission()) {
                GoodsAddStorageActivity.start(StorageManageActivity.this, storageManageCallBackEntity.getStorageManageEntity());
            }
        });

        mViewModel.showNoDataView.observe(this, integer -> mViewModel.showNoData(mDataBinding.contentContainer));
    }

    private void initView() {
        mDataBinding.headContainer.title.setText("仓储管理");
    }

    private boolean isHavingPermission() {
//        if(BuildConfig.IS_MODULE_DEBUG) {
//            return true;
//        } else {
//            Object object = GoodsModule.getInstance().call2("getStorageOperationPermission");
//            return null != object && (Boolean) object;
//        }
        return true;
    }

    private void showDialog(String id, int position) {
        View view = View.inflate(StorageManageActivity.this, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(StorageManageActivity.this, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);
        TextView tvMessage = view.findViewById(R.id.tv_message);
        TextView tvLogoutConfirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tvLogoutCancel = view.findViewById(R.id.tv_logout_cancel);
        tvMessage.setText("确定要删除吗?");
        tvLogoutConfirm.setOnClickListener(v -> {
            roundCornerDialog.dismiss();
            mViewModel.delete(id, position);
        });
        tvLogoutCancel.setOnClickListener(v -> roundCornerDialog.dismiss());
    }
}
